package com.compass.mobile.ui.viewModel

import androidx.lifecycle.MutableLiveData
import com.compass.mobile.framework.utils.TipsToast
import com.compass.mobile.models.ArticleBean
import com.compass.mobile.models.IndependentBillingBean
import com.compass.mobile.network.callback.IApiErrorCallback
import com.compass.mobile.network.manager.ApiManager
import com.compass.mobile.network.viewmodel.BaseViewModel
import com.google.gson.JsonObject
import stringListToJson

/**
 * Description： 自主开单
 * Date:2024/6/4 14:53
 */
class IndependentBillingViewModel : BaseViewModel() {
    val inspectionListLiveData = MutableLiveData<MutableList<IndependentBillingBean>?>()
    val submitInspectionLiveData = MutableLiveData<Any?>()


    val selectedListLiveData = MutableLiveData<MutableList<IndependentBillingBean>>()
    var selectedList: MutableList<IndependentBillingBean> = mutableListOf()//已选的集合
    private var testList: MutableList<IndependentBillingBean> = mutableListOf()//检验
    private var examinationList: MutableList<IndependentBillingBean> = mutableListOf()//检查


    /**
     * @param type 1是检验 2是检查
     */
    fun getInspectionList(type: String?) {
        when (type) {
            "1" -> {
                if (testList.isEmpty()) {
                    getInspectionListForNetWork(type)
                } else {
                    inspectionListLiveData.value = testList
                }
            }

            "2" -> {
                if (examinationList.isEmpty()) {
                    getInspectionListForNetWork(type)
                } else {
                    inspectionListLiveData.value = examinationList
                }
            }
        }
    }


    private fun getInspectionListForNetWork(type: String?) {


        launchUIWithResult(responseBlock = {
            ApiManager.api.getInspectionProjectList(type)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                TipsToast.showTips(error)
            }
        }, successBlock = {
            val inspectList: MutableList<IndependentBillingBean> = mutableListOf()//包装集合
            it?.let { map ->
                for (en in map.entries) {
                    inspectList.addAll(0, en.value)
                }
                when (type) {
                    "1" -> {
                        testList = inspectList
                        inspectionListLiveData.value = testList
                    }

                    "2" -> {
                        examinationList = inspectList
                        inspectionListLiveData.value = examinationList
                    }
                }

            }
        })
    }


//    private fun getInspectionListForNetWork(type: String?) {
//
//
//        launchUIWithResult(responseBlock = {
//            ApiManager.api.getInspectionProjectList("2")
//        }, errorCall = object : IApiErrorCallback {
//            override fun onError(code: Int?, error: String?) {
//                TipsToast.showTips(error)
//            }
//        }, successBlock = {
//
//            it?.let { map ->
//                for (en in map.entries) {
//                    if (en.key=="检验项目"){
//                        testList = en.value
//                    }
//                    if (en.key=="检查项目"){
//                        examinationList = en.value
//                    }
//                }
//                when (type) {
//                    "1" -> {
//                        inspectionListLiveData.value = testList
//                    }
//
//                    "2" -> {
//                        inspectionListLiveData.value = examinationList
//                    }
//                }
//
//            } ?: kotlin.run {
//                inspectionListLiveData.value = null
//
//            }
//        })
//    }
//

    fun submitInspection(
        patientId: String,
        list: ArrayList<String>?
    ) {

        val params = JsonObject().apply {
            addProperty("patientId", patientId)

            add("inspectionIds", stringListToJson(list))
        }

        launchUIWithResult(responseBlock = {
            ApiManager.api.submitInspection(params)
        }, errorCall = object : IApiErrorCallback {
            override fun onError(code: Int?, error: String?) {
                super.onError(code, error)
                submitInspectionLiveData.value = null
            }
        }) {
            submitInspectionLiveData.value = it
        }
    }

}